package com.enzoism.springboot.dive.sse.controller;

import com.enzoism.springboot.dive.sse.service.DeepSeekService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
public class ChatController {

    private final DeepSeekService deepSeekService;

    public ChatController(DeepSeekService deepSeekService) {
        this.deepSeekService = deepSeekService;
    }

    /**
     * 使用单个线程-进行
     * @param message
     * @return
     */
    @GetMapping("/chat")
    public SseEmitter chat(@RequestParam String message) {
        SseEmitter emitter = new SseEmitter();
        ExecutorService executorService = Executors.newSingleThreadExecutor();

        executorService.submit(() -> {
            try {
                // 调用DeepSeek的chat模型
                deepSeekService.streamChatResponse(message, emitter);
            } catch (Exception e) {
                emitter.completeWithError(e);
            } finally {
                executorService.shutdown();
            }
        });

        return emitter;
    }
}